home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickTime / Easy Video Grabber / BigEasyVideoGrabber.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  2.9 KB  |  158 lines  |  [TEXT/KAHL]

  1. /* file: BigEasyVideoGrabber.c
  2.  *
  3.  * Started 18 December 1991
  4.  *
  5.  * A set of routines for using the
  6.  * somewhat baroque and, perhaps, spurious
  7.  * video-digitizer and sequence-grabber
  8.  * component interfaces.
  9.  *
  10.  * It implements the one straight-ahead
  11.  * routine: GrabFrame.
  12.  *
  13.  */
  14.  
  15.  
  16.  
  17. /*--------------------------
  18.     Inclusions
  19. --------------------------*/
  20.  
  21. #include <Memory.h>
  22. #include <OSUtils.h>
  23. #include "BigEasyVideoGrabber.h"
  24.  
  25.  
  26. /*--------------------------
  27.     Routines
  28. --------------------------*/
  29.  
  30.  
  31. EasyVideoGrabber NewEasyVideoGrabber(Rect *outputSize)
  32. /*
  33.  * Gets the default video digitizer,
  34.  * and returns the output size of the video image.
  35.  */
  36.     {
  37.     EasyVideoGrabber evg;
  38.     ComponentResult thisError;
  39.  
  40.     evg = (void *)NewPtr(sizeof(EasyVideoGrabberRecord));
  41.     if(!evg)
  42.         goto fail;
  43.  
  44.     evg->sg = 0;
  45.     evg->vc = 0;
  46.  
  47.     evg->sg = OpenDefaultComponent(SeqGrabComponentType, 0);
  48.     if(!evg->sg)
  49.         goto fail;
  50.  
  51.     thisError = SGInitialize(evg->sg);
  52.     if(thisError)
  53.         goto fail;
  54.  
  55.     thisError = SGNewChannel(evg->sg, VideoMediaType, &evg->vc);
  56.     if(thisError)
  57.         goto fail;
  58.  
  59.     if(outputSize)
  60.         {
  61.         thisError = SGGetSrcVideoBounds(evg->vc, outputSize);
  62.         if(thisError)
  63.             goto fail;
  64.         }
  65.  
  66.     thisError = SGSetChannelUsage(evg->vc, seqGrabPreview);
  67.     if(thisError)
  68.         goto fail;
  69.  
  70.     goto goHome;
  71.  
  72. fail:
  73.     if(evg)
  74.         {
  75.         if(evg->sg)
  76.             CloseComponent(evg->sg);
  77.         DisposePtr((void *)evg);
  78.         evg = 0;
  79.         }
  80.  
  81. goHome:
  82.     return evg;
  83.     }
  84.  
  85.  
  86. Boolean GrabEasyVideoGrabber(EasyVideoGrabber evg,Rect *r)
  87. /*
  88.  * Grab a frame, and draw it in the current port, of size 'r'.
  89.  * You can pass 'nil' for the EasyVideoGrabber, and one
  90.  * will be allocated and disposed, while-u-wait.
  91.  *
  92.  * The boolean returned is 'true' iff a frame was actually captured and drawn.
  93.  *
  94.  * The 'while' loop is a real barnstorming technique. The "Sequence Grab"
  95.  * component does not provide any way to know when a frame has actually
  96.  * been grabbed. Therefore, the Sequence Grabber is simply idled for
  97.  * a few ticks, on the _assumption_ that the video frame rate is high
  98.  * enough that at least one frame is grabbed.
  99.  *
  100.  * While this may seem mildly inelegant, it, in fact, works.
  101.  */
  102.     {
  103.     ComponentResult thisError;
  104.     GWorldPtr gw;
  105.     GDHandle gd;
  106.     Boolean localGrabber;
  107.     unsigned long t;
  108.     Boolean result;
  109.  
  110.     result = false;
  111.  
  112.     localGrabber = (evg == 0);
  113.     if(localGrabber)
  114.         evg = NewEasyVideoGrabber(nil);
  115.  
  116.     if(!evg)
  117.         goto goHome;
  118.  
  119.     GetGWorld(&gw,&gd);
  120.  
  121.     thisError = SGSetGWorld(evg->sg,gw,gd);
  122.     if(thisError)
  123.         goto goHome;
  124.  
  125.     thisError = SGSetChannelBounds(evg->vc,r);
  126.     if(thisError)
  127.         goto goHome;
  128.  
  129.     thisError = SGStartPreview(evg->sg);
  130.     if(thisError)
  131.         goto goHome;
  132.  
  133.     t = TickCount() + 8;
  134.     do
  135.         {
  136.         thisError = SGIdle(evg->sg);
  137.         if(thisError)
  138.             goto goHome;
  139.         } while (TickCount() < t);
  140.  
  141.     thisError = SGStop(evg->sg);
  142.  
  143.     if(localGrabber)
  144.         DisposeEasyVideoGrabber(evg);
  145.  
  146.     result = true;
  147.  
  148. goHome:;
  149.     return result;
  150.     }
  151.  
  152. void DisposeEasyVideoGrabber(EasyVideoGrabber evg)
  153.     {
  154.     CloseComponent(evg->sg);
  155.     DisposePtr( (void *)evg);
  156.     }
  157.  
  158.